/*
* License:  This  program  is  free  software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published by
* the  Free Software Foundation; either version 3 of the License, or (at your
* option)  any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*/

package pcgen.CharacterViewer.resources;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Parcel;
import android.os.Parcelable;

import pcgen.android.Logger;

public class GameResourceHistory extends GameResourceParcelable
{
	public GameResourceHistory()
	{	
	}
	
	public GameResourceHistory(Parcel in)
	{
		super(in);
	}
	
	public int getRound()
	{
		return _round;
	}
	
	public JSONObject load(JSONObject data)
		throws JSONException
	{
		setRound(data.getInt("round"));

		_items.clear();
		JSONArray items = data.getJSONArray("items");
		for (int index = 0; index < items.length(); index++)
		{
			if (items.isNull(index))
				continue;
			
			JSONObject itemJSON = items.getJSONObject(index);
			
			GameResourceHistoryItem item = new GameResourceHistoryItem();
			item.load(itemJSON);
			
			_items.add(item);
		}
	    
	    return data;
	}
	
	public JSONObject save() 
		throws JSONException
	{
	    JSONObject data = new JSONObject();
	    
	    data.put("round", getRound());
		
		JSONArray itemsJSON = new JSONArray();
		for (GameResourceHistoryItem item : _items)
		{
			JSONObject itemJSON = item.save();
			itemsJSON.put(itemJSON);
		}
		
		data.put("items", itemsJSON);
	    
	    return data;
	}
	
	public void setRound(int value)
	{
		_round = value;
	}
	
	public static final Parcelable.Creator<GameResourceHistory> CREATOR = new Parcelable.Creator<GameResourceHistory>() 
	{
		public GameResourceHistory createFromParcel(Parcel in) 
		{
		    return new GameResourceHistory(in);
		}
		
		public GameResourceHistory[] newArray(int size) 
		{
		    return new GameResourceHistory[size];
		}
	};

	@Override
	protected void readFromParcelTransform(Parcel in) 
		throws Throwable
	{
		try
    	{
			setRound(in.readInt());
			
			_items = in.readParcelable(GameResourceHistoryItemList.class.getClassLoader());
		}
    	catch (Throwable tr)
    	{
    		Logger.e(TAG, "readFromParcelTransform", tr);
    		throw tr;
    	}
	}

	@Override
	protected void writeToParcelTransform(Parcel dest, int flags)
		throws Throwable
	{
		try
    	{
			dest.writeInt(getRound());
			
			dest.writeParcelable(_items, flags);
		}
    	catch (Throwable tr)
    	{
    		Logger.e(TAG, "writeToParcelTransform", tr);
    		throw tr;
    	}
	}
	
	private int _round;
	
	private GameResourceHistoryItemList _items = new GameResourceHistoryItemList();

	private static final String TAG = GameResourceHistory.class.getSimpleName();
}
